package com.vasthu.core.service;

import com.vasthu.api.dao.UserDAO;
import com.vasthu.api.domain.User;
import com.vasthu.api.service.UserManagerService;
import com.vasthu.core.dao.UserDAOImpl;
import com.vasthu.gui.VstTableItemSelectable;
import com.vasthu.utility.HashUtil;
import com.vasthu.utility.VstDBManager;
import java.util.*;
import java.sql.*;
import org.apache.log4j.Logger;

public class UserManagerServiceImpl implements UserManagerService {

    private UserDAO userDao;
    static Logger logger = Logger.getLogger(UserManagerServiceImpl.class);

    public UserManagerServiceImpl() {
        userDao = new UserDAOImpl();
    }

    @Override
    public boolean insertUser(User user) {
        if (user == null) {
            return false;
        }
        boolean ok = userDao.insertUser(user);
        return ok;
    }

    @Override
    public boolean updateUser(User user) {
        if (user == null) {
            return false;
        }
        boolean ok = userDao.updateUser(user);
        return ok;
    }

    @Override
    public boolean deleteUserById(long userId) {
        boolean ok = userDao.deleteUserById(userId);
        return ok;
    }

    public List<VstTableItemSelectable> getAllUsersForTable() {
        List<User> userList = new ArrayList<User>();
        List<VstTableItemSelectable> dataList = new ArrayList<VstTableItemSelectable>();
        userList = userDao.getAllUsers();
        for (User user : userList) {
            dataList.add(user);
        }
        return dataList;
    }

    @Override
    public User authenticate(String username, String password) {
        User user = userDao.getUserByUserName(username);
        if (user != null && user.getUserPassword().equals(HashUtil.hashString(password))) {
            return user;
        }
        return null;
    }
}